package com.leyou.auth.config;

import com.leyou.common.auth.utils.RsaUtils;
import com.leyou.common.enums.LyExceptionEnum;
import com.leyou.common.exception.LyException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.security.PrivateKey;
import java.security.PublicKey;
@Slf4j
@Data
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties implements InitializingBean{
    // 该类实现InitializingBean ,可以调用它的方法,在给属性赋值后,再生成需要的对象

    //使用配置类1.将配置类交给Spring管理.2.在启动类上只用@EnableConfigurationProperties(JwtProperties.class)

    /**
     * 公钥路径
     */
    private String pubKeyPath;
    /**
     * 私钥路径
     */
    private String priKeyPath;

    /**
     * 公钥对象
     */
    private PublicKey publicKey;
    /**
     * 私钥对象
     */
    private PrivateKey privateKey;

    private UserTokenProperties user = new UserTokenProperties();

    @Data
    public class UserTokenProperties{
        private int expire;
        private String cookieName;
        private String cookieDomain;
        private int refreshTime;
    }

//对象创建后再给属性赋值
    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            publicKey = RsaUtils.getPublicKey(pubKeyPath);
            privateKey=RsaUtils.getPrivateKey(priKeyPath);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【授权中心】获取公钥和私有失败",e);
            //@Slf4j 使用该注解(日志)
            throw new LyException(LyExceptionEnum.INVALID_PARAM_ERROR);
        }
    }
}
